package org.ywf.agent.consumer;

import com.alibaba.fastjson.JSON;
import com.sun.corba.se.impl.orbutil.threadpool.TimeoutException;
import org.springframework.util.StringUtils;
import org.ywf.common.entity.ConsumerDto;
import org.ywf.common.entity.RequestData;
import org.ywf.common.enums.ResponseStatus;
import org.ywf.framework.annotation.ProxyMapping;
import org.ywf.framework.core.Consumer;
import org.ywf.web.entity.JHApiResult;

/**
 * 挡板配置层
 */
public abstract class AbstractAbcConsumer implements Consumer {

    @Override
    public ConsumerDto communicate(ProxyMapping mapping, Object serverBody, RequestData<?> requestData, Class returnType) throws TimeoutException {
        ConsumerDto consumerDto = new ConsumerDto();
        String response = "";
        Object result = "";
        System.out.println("挡板配置层");
        String serviceCode = mapping.serviceCode();
        //判断不是挡板
        if (true){
            response = this.dataCommit(serviceCode,requestData);
            if (!StringUtils.isEmpty(response)){
                 JHApiResult jhApiResult = JSON.parseObject(response, JHApiResult.class);
                if (jhApiResult != null && jhApiResult.getErrorCode().equals("0")){
                    result = JSON.parseObject(jhApiResult.getResult(), returnType);
                    consumerDto.setStatus(ResponseStatus.SUCCESS);
                }else{
                    consumerDto.setStatus(ResponseStatus.FAILD);
                }
            }else{
                throw new NullPointerException("服务端无数据返回");
            }
        }
        consumerDto.setBody(result);
        consumerDto.setServerReq(requestData.getBody());
        consumerDto.setServerRes(response);
        return consumerDto;
    }

    protected abstract String dataCommit(String serviceCode,RequestData<?> requestData);
}
